package com.hspedu.homework.homework01;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/3/3
 * @time 17:33
 */
public class Homework01Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        //获取到创建连接的服务端的 tcp socket
        Socket accept = serverSocket.accept();
        InputStream inputStream = accept.getInputStream();
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        int readLen = 0;
        String question = null;
        byte[] buffer = new byte[512];
        while ((readLen = bufferedInputStream.read(buffer)) != -1){
            question = new String(buffer, 0, readLen);
            System.out.println(question);
        }


        if ("name".equals(question)){

            OutputStream outputStream = accept.getOutputStream();
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
            byte[] buf = "我是nova".getBytes();
            bufferedOutputStream.write(buf,0,buf.length);
            bufferedOutputStream.flush();
            accept.shutdownOutput();

        }else if ("hobby".equals(question)){

            OutputStream outputStream = accept.getOutputStream();
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
            byte[] buf = "编写Java程序".getBytes();
            bufferedOutputStream.write(buf,0,buf.length);
            bufferedOutputStream.flush();
            accept.shutdownOutput();

        }else{

            OutputStream outputStream = accept.getOutputStream();
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
            byte[] buf = "你说啥呢".getBytes();
            bufferedOutputStream.write(buf,0,buf.length);
            bufferedOutputStream.flush();
            accept.shutdownOutput();

        }

        accept.close();
        serverSocket.close();

    }
}
